package com.jzo2o.market.controller.inner;

import com.jzo2o.api.market.CouponApi;
import com.jzo2o.api.market.dto.request.CouponUseBackReqDTO;
import com.jzo2o.api.market.dto.request.CouponUseReqDTO;
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
import com.jzo2o.api.market.dto.response.CouponUseResDTO;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponUseBackService;
import com.jzo2o.market.service.ICouponWriteOffService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 内部接口 - 优惠卷管理控制器
 * @author JIAN
 */
@Slf4j
@RestController("innerCouponController")
@RequestMapping("/inner/coupon")
public class CouponController implements CouponApi {
    @Resource
    private ICouponService couponService;
    @Resource
    private ICouponWriteOffService couponWriteOffService;
    @Resource
    private ICouponUseBackService couponUseBackService;

    /**
     * 根据订单金额获取当前用户可用优惠卷
     */
    @Override
    @GetMapping("/getAvailable")
    @ApiOperation("获取可用优惠券列表")
    @ApiImplicitParam(name = "totalAmount", value = "总金额，单位分", required = true, dataTypeClass = BigDecimal.class)
    public List<AvailableCouponsResDTO> getAvailableCoupon(@RequestParam BigDecimal totalAmount) {
        return couponService.getAvailableCoupon(totalAmount);
    }

    @Override
    @PostMapping("/use")
    @ApiOperation("使用优惠卷并返回优惠金额")
    public CouponUseResDTO useCoupon(@RequestBody CouponUseReqDTO couponUseReqDTO) {
        return couponWriteOffService.use(couponUseReqDTO);
    }

    @Override
    @PostMapping("/useBack")
    @ApiOperation("优惠券退回接口")
    public void useBack(@RequestBody CouponUseBackReqDTO couponUseBackReqDTO) {
        couponUseBackService.useBack(couponUseBackReqDTO);
    }
}